# IMPORTAR DADOS
ifelse(!require(readxl),install.packages("readxl", dependencies=TRUE),1)
require(readxl)
Dataset <- read_excel("diret�rio") #incluir diret�rio
View(Dataset)

#PADRONIZAR VARI�VEL ICGE
Dataset$ICGE_S=(Dataset$ICGE-mean(Dataset$ICGE))/sd(Dataset$ICGE)

#CRIAR TERMOS DE INTERA��O LID X ICGE_S E ENG X ICGE_S
Dataset$Mod_S_1=Dataset$ENG1*Dataset$ICGE_S
Dataset$Mod_S_2=Dataset$ENG2*Dataset$ICGE_S
Dataset$Mod_S_3=Dataset$ENG3*Dataset$ICGE_S
Dataset$Mod_S_4=Dataset$ENG4*Dataset$ICGE_S
Dataset$Mod_S_5=Dataset$ENG5*Dataset$ICGE_S
Dataset$Mod_S_6=Dataset$LID1*Dataset$ICGE_S
Dataset$Mod_S_7=Dataset$LID2*Dataset$ICGE_S
Dataset$Mod_S_8=Dataset$LID3*Dataset$ICGE_S
Dataset$Mod_S_9=Dataset$LID4*Dataset$ICGE_S
Dataset$Mod_S_10=Dataset$LID5*Dataset$ICGE_S
Dataset$Mod_S_11=Dataset$LID6*Dataset$ICGE_S
Dataset$Mod_S_12=Dataset$LID7*Dataset$ICGE_S
Dataset$Mod_S_13=Dataset$LID8*Dataset$ICGE_S
Dataset$Mod_S_14=Dataset$LID9*Dataset$ICGE_S
Dataset$Mod_S_15=Dataset$LID10*Dataset$ICGE_S
Dataset$Mod_S_16=Dataset$LID11*Dataset$ICGE_S
Dataset$Mod_S_17=Dataset$LID12*Dataset$ICGE_S

#DETERMINAR O DIAGRAMA DE CAMINHO
ge=c(0,0,0,0,0,0)
mod=c(0,0,0,0,0,0)
icge=c(0,0,0,0,0,0)
eng=c(1,0,0,0,0,0)
lid=c(1,0,0,0,0,0)
npb=c(1,1,1,0,0,0)

#CRIAR A MATRIZ DE CAMINHO
path_escola=rbind(ge,mod,icge,eng,lid,npb)

#PLOTAR MODELO
innerplot(path_escola)

#RELACIONAR INDICADORES
blocks_escola=list(2:18,21:37,20,2:6,7:18,1)
blocks_escola_lid12=list(2:17,21:36,20,2:6,7:17,1) #excluindo-se a vari�vel LID12

#DETERMINAR TIPO DE CONSTRUTO SENDO "A" REFLEXIVO E "B' FORMATIVO
modes_escola=c("A","A","A","A","A","B")

#CARREGAR PACOTE PLMPM
ifelse(!require(plspm),install.packages("plspm", dependencies=TRUE),1)
require(plspm)

#RODAR PLSPM (1 fase)
pls_escola=plspm(Dataset, path_escola, blocks_escola,modes= modes_escola, scheme = "path", scaled = NULL, tol = 1e-07, maxiter = 300, boot.val=TRUE, br=5000)

#RESULTADOS (1 fase)
pls_escola$unidim [-c(1:3,6),]
pls_escola$outer_model[-c(1:35,53),-c(3,6)]
pls_escola$inner_summary[-c(1:3,6),5, drop = FALSE]
sqrt(pls_escola$inner_summary$AVE)
summary(pls_escola)

#RODAR PLSPM (2 fase)
pls_escola_lid12=plspm(Dataset, path_escola, blocks_escola_lid12,modes= modes_escola, scheme = "path", scaled = NULL, tol = 1e-07, maxiter = 300, boot.val=TRUE, br=5000)

#RESULTADOS (2 fase)
pls_escola_lid12$unidim [-c(1:3,6),]
pls_escola_lid12$outer_model[-c(1:33,50),-c(3,6)]
pls_escola_lid12$inner_summary[-c(1:3,6),5, drop = FALSE]
sqrt(pls_escola_lid12$inner_summary$AVE)
pls_escola_lid12$crossloadings[-c(1:33,50),-c(3:5,8)]
summary(pls_escola_lid12)
# RESULTADOS: CRIT�RIO DE FORNELL-LACKER (validade discriminante)
sqrt(pls_escola$inner_summary$AVE) # Raiz quadrada da AVE

#PLOTAR MODELOS
  #ESTRUTURAL
plot(pls_escola_lid12)
  #REFLEXIVO
plot(pls_escola_lid12, what = "loadings", arr.width = 0.1)
  #FORMATIVO
plot(pls_escola_lid12, what = "weights", arr.width = 0.1)